<template>
    <van-list
        v-if="!is_empty"
        v-model="loading"
        :finished="is_finished"
        :offset="300"
				:immediate-check="false"
        finished-text="没有更多了"
				@load="doLoad"
    >
        <prod-item :list="list" />
    </van-list>
    <no-data :text="no_data_text" v-else />
</template>
<script>
import {
	List
} from 'vant'
import NoData from '@cmpt/no-data'
import ProdItem from './item'

import {
    getProdList
} from '@/api/modules/product'
import {
  getPageName,
} from '@router/path'

	export default {
		name: 'prod-list',
		props: {
			params: Object,
			no_data_text: String
		},
		components: {
			[List.name]: List,
			[ProdItem.name]: ProdItem,
			[NoData.name]: NoData
		},
		data () {
			return {
				list: [],
				total: 0,
				page: 1,
				limit: 10,
				loading: false,
				is_finished: false,
				is_empty: false,
			}
		},
		watch: {
			params: {
				handler(newValue, oldValue) {
					this.reLoad()
				},
				deep: true
			}
		},
		created() {
			this.doLoad()
			this.bindWxShare()
		},
		methods: {
			bindWxShare(){
				// 设置微信、app分享：产品列表页
				if(getPageName() !== 'product/list')
					return
				const kindName = (this.params || {}).kind_name || '全部'
				this.$set_share({
					title: `优采${kindName === '全部' ? '' : kindName}产品列表`,
				})
			},
			reLoad () {
					this.list = []
					this.total = 0
					this.page = 1
					this.loading = false
					this.is_finished = false
					this.is_empty = false
					this.doLoad()
					this.bindWxShare()
			},
			async doLoad() {
				this.loading = true
				let params = {}
				localStorage.latest_city_code && (params.region_id = Number(localStorage.latest_city_code))
				if(this.params.kind || this.params.kind === 0)
					params.kind = this.params.kind
				this.params.keyword && (params.keywords = this.params.keyword)
				this.params.area && (params.zone_id = this.params.area)
				this.params.sort && (params.sort = this.params.sort)
				params.page = this.page
				getProdList(params)
					.then(res => {
						this.list = this.list.concat(res.data.list)
						this.total = res.data.total

						this.is_finished = this.total <= this.list.length
						this.loading = false
						!this.is_finished && (this.page++)
						this.is_empty = this.total <= 0
					})
					.catch(err => {
						this.is_finished = this.total <= this.list.length
						this.loading = false
						this.is_empty = this.total <= 0
					})
			}
		}
	}
</script>